package com.xlh.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.Utils.FontHandler;
import com.xlh.bean.ProBean;
import com.xlh.bean.TitleBean;
import com.xlh.outside.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import net.nightwhistler.htmlspanner.HtmlSpanner;

@SuppressLint("ViewHolder")
public class NPCAdapter extends BaseAdapter {

	private List<TitleBean> data;
	private LayoutInflater mInflater;
	private ProBean proStatus;

	ViewHolder holder;

	@SuppressLint("UseSparseArrays")
	public NPCAdapter(Context context, List<TitleBean> data) {
			// TODO Auto-generated constructor stub
			mInflater = LayoutInflater.from(context);
			this.data = data;
		choiceItems = new HashMap<Integer, List<ProBean>>();
	}

	private Map<Integer, List<ProBean>> choiceItems;

	// 添加进度条
	public void updateProgress(int position, List<ProBean> ps) {
		//Log.e("coco", "weizhi : " + position + "###ge shu " + ps.size());
		//Log.e("测试后",data.get(position).getmAction());
		for (int i = 0; i < ps.size(); i++) {
			Log.e("coco", "Max:" + ps.get(i).getMax() + "~~~Min:" + ps.get(i).getMin());
		}
		choiceItems.put(position, ps);




		this.notifyDataSetChanged();
	}

	// 添加进度条
	public void updateProgress(int pos,ProBean ps) {
		data.get(pos).setProStatus(ps);
		this.notifyDataSetChanged();
	}

	// 添加进度条
	public void updateProgress(ProBean ps) {
		this.proStatus = ps;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data == null ? 0 : data.size();
	}

	public TitleBean getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {



		if (convertView == null) {

			convertView = mInflater.inflate(R.layout.npc_item, parent, false);
			holder = new ViewHolder();
			holder.tvName = (TextView) convertView.findViewById(R.id.title_text);
			holder.pro01 = (ProgressBar) convertView.findViewById(R.id.pro01);
			//holder.pro02 = (ProgressBar) convertView.findViewById(R.id.pro02);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

//		换图片APP时注释的 如果不要图片模式的这些需要打开
//		GradientDrawable drawable = (GradientDrawable) holder.tvName.getBackground();
//		//设置边框
//		drawable.setStroke(FUti.dip2px(convertView.getContext(),1), Color.parseColor(Constant.getAppColor().getButBorderColor()));
//		//设置背景
//		drawable.setColor(Color.parseColor(Constant.getAppColor().getButBackColor()));

		TitleBean titleBean = data.get(position);
		holder.tvName.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor() ));
		//FontHandler fth = new FontHandler(convertView.getContext());

		holder.tvName.setText(titleBean.getSpanned());


		//先全部隐藏血条
		holder.pro01.setVisibility(View.GONE);

		if(titleBean.getProStatus()!=null){
			holder.pro01.setVisibility(View.VISIBLE);
			holder.pro01.setMax(Integer.parseInt(titleBean.getProStatus().getMax()));
			holder.pro01.setProgress(Integer.parseInt(titleBean.getProStatus().getMin()));
		}


		return convertView;
	}

	private class ViewHolder {

		private ProgressBar pro01;
		private ProgressBar pro02;
		private TextView tvName;
	}

	public void addTitle(TitleBean bean) {
		// TODO Auto-generated method stub
		if (data != null) {
			data.add(bean);
		}
		this.notifyDataSetChanged();
	}

	public void setData(List<TitleBean> mTitleData) {
		// TODO Auto-generated method stub
		if (mTitleData != null) {
			this.data = mTitleData;
		}
		this.notifyDataSetChanged();
	}

	public void clear() {
		// TODO Auto-generated method stub
		if (data != null) {
			data.clear();
		}
		this.notifyDataSetChanged();
	}

	public void addTitle(List<TitleBean> ts) {
		// TODO Auto-generated method stub

		if (this.data != null) {
			for (int i = 0; i < ts.size(); i++) {
				this.data.add(ts.get(i));
			}
		}
		this.notifyDataSetChanged();
	}
}
